package com.dzt.androidframework.network.mobapi.cook;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.TitleFrameActivity;
import com.dzt.androidframework.databinding.ActivityCookBinding;
import com.dzt.androidframework.photo.PhotoViewActivity;
import com.dzt.androidframework.utils.GsonUtils;

import java.util.List;

public class CookActivity extends TitleFrameActivity<ActivityCookBinding>
		implements CookView {

	private CookPresenter presenter;
	private Cook cook;

	public class OnClickHandler {
		public void onThumbnailClick(View view) {
			if (!TextUtils.isEmpty(cook.getResult().getRecipe().getImg())) {
				Intent intent = new Intent();
				intent.putExtra("url", cook.getResult().getRecipe().getImg());
				intent.putExtra("name", cook.getResult().getName());
				startActivity(PhotoViewActivity.class, intent);
			}
		}
	}

	@Override
	protected void initData(Bundle savedInstanceState) {
		presenter = new CookPresenter();
		presenter.onCreate();
		presenter.attachView(this);
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_cook;
	}

	@Override
	protected void initWidgets() {
		showLoading();
		bindingView.setHandler(new OnClickHandler());
		setTitle("菜谱详情");
		String cid = getIntent().getStringExtra("cid");
		presenter.queryCook(cid);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		presenter.onDestroy();
	}

	@Override
	public void onSuccess(Cook cook) {
		showContentView();
		this.cook = cook;
		setTitle(cook.getResult().getName());
		bindingView.setResult(cook.getResult());

		String ingredient = cook.getResult().getRecipe().getIngredients();
		List<String> ingredients = GsonUtils.jsonToList(ingredient, String.class);
		if (ingredients == null) {
			bindingView.tvIngredients.setVisibility(View.GONE);
		} else {
			String ing = "";
			for (String m : ingredients) {
				ing += m + "\n";
			}
			bindingView.tvIngredients.setText(ing);
		}

		String method = cook.getResult().getRecipe().getMethod();
		List<Method> list = GsonUtils.jsonToList(method, Method.class);
		if (list == null) {
			bindingView.llMethod.setVisibility(View.GONE);
		} else {
			for (Method m : list) {
				if (!TextUtils.isEmpty(m.getImg())) {
					ImageView imageView = new ImageView(context);
					Glide.with(context).load(m.getImg())
							.placeholder(R.mipmap.ic_launcher)
							.crossFade()
							.error(R.drawable.pic_loading)
							.into(imageView);
					bindingView.llMethod.addView(imageView);
				}
				if (!TextUtils.isEmpty(m.getStep())) {
					TextView textView = new TextView(context);
					textView.setText(m.getStep());
					textView.setTextSize(18.0f);
					bindingView.llMethod.addView(textView);
				}
			}
		}
	}

	@Override
	public void onError(String result) {
		showError();
	}

	@Override
	protected void onRefresh() {
		super.onRefresh();
		String cid = getIntent().getStringExtra("cid");
		presenter.queryCook(cid);
	}
}
